package dp.mobile.eden;

import android.content.Intent;
import android.content.SharedPreferences;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import dp.mobile.entity.generator.GeneratorFactory;
import dp.mobile.entity.saveable.SaveablePlayer;
import dp.mobile.game.Game;
import dp.mobile.game.GameEventListener;
import dp.mobile.game.GameTouchListener;
import dp.mobile.game.TutorialGame;
import dp.mobile.glwrapper.GameRenderer;
import dp.mobile.panel.PanelTouchListener;
import dp.mobile.system.Utilities;

public class CampaignAct extends GameActivityBase implements GameEventListener {
	private static final String TAG = "Campaign Act";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);

		//initialize components
		initComp();
		// initialize game
		String levelSource = null;
		if (getIntent().getExtras() == null || (levelSource = getIntent().getExtras().getString("level")) != null) {
			if (!levelSource.equals("1.xml") && !levelSource.equals("tutorial.xml"))
				mGame = new Game(levelSource);
			else
				mGame = new TutorialGame("1.xml");
			
			mGame.setListener(this);
		}
		else if (getIntent().getExtras().getBoolean(EXTRA_CONTINUE)) {
			Log.d(TAG, "factory = null ? " + GeneratorFactory.instance().getImage().Loaded);
			mGame = Game.createResumeGame();
			if (mGame == null)
				finish();
			else {
				mGame.setListener(this);
			}
		}
		
		// initialize screen
		mSurface = (GLSurfaceView)findViewById(R.id.game_screen);
		mSurface.setRenderer(GameRenderer.instance(this, mGame));
		
		// touch handling
		mGameTouchListener	= new GameTouchListener(mGame);
		mPanelTouchListener	= new PanelTouchListener(this, mGame, mTransparentPanel);
		setListener();
		setPausePanelListeners();
		
		//Log.i("this" , mGame.getTowers().size() + "");
		
		// let the game begin!!
		if (mGame != null) {
			mAvailableTowers = mGame.getAvailableTowers();
			mGame.start();
		}
		
		mSaveGameButton.setVisibility(View.GONE);
	}
	
	@Override
	public void onGameOver() {
		if (!isGameOver) {
			if (mGame.getPlayer().getLife() <= 0) {
				SaveablePlayer p = mGame.getPlayer().createSaveable();
				Intent i = new Intent(this, GameOverAct.class);
				i.putExtra(EXTRA_PLAYER_STAT, p);
				i.putExtra(GameOverAct.EXTRA_CAMPAIGN, true);
				i.putExtra(GameOverAct.EXTRA_LEVEL_SOURCE, mGame.getLevelSource());
				startActivity(i);
			}
			finish();
			isGameOver = true;
		}
	}
	
	@Override
	public void onWin(int score) {
		super.onWin(score);

		Log.d(TAG, "Shared pref nya berubah loh");
		
		SharedPreferences pref = getSharedPreferences(Utilities.PREFERENCES, 0);
		int lastAvailable = pref.getInt(Utilities.LAST_LEVEL_PREF, 1);
		SharedPreferences.Editor edit = pref.edit();
		edit.putInt(Utilities.LAST_LEVEL_PREF, lastAvailable + 1);
		edit.commit();
	}
	
	public static final String EXTRA_CONTINUE = "continue_campaign";
	public static final String EXTRA_TUTORIAL = "tutorial";
}
